package com.cn.medicalcare.controller.backstageservlet.imgservlet;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.service.UpLoadImgService;
import com.cn.medicalcare.service.serviceimpl.UpLoadImgServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/uploadimg.json")
@MultipartConfig(maxFileSize = 5 * 1024 * 1024)
public class UploadImgServlet extends HttpServlet {

    private UpLoadImgService upLoadImgService = UpLoadImgServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> map = new LinkedHashMap<>();
        //保存上传的文件
        Part part = req.getPart("file");
        if (part != null) {
            String filepathandpath = upLoadImgService.upLoadImg(part);
            if (filepathandpath != null) {
                map.put("status", 200);
                map.put("src",filepathandpath);
            } else {
                map.put("status", 500);
            }
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
